﻿package com.bizmlib.utils.array
{
	public class ArrayHandling extends Object
	{
	
		public function ArrayHandling()
		{
		}
	
	
		static public function toString():String
		{
			return "[com.bizmlib.utils.array.ArrayHandling]";
		}
	
	
		static public function isItemInArray(array:Array, item:Object):Boolean
		{
			for(var i:Number=0; i<array.length; i++){
				if(item==array[i])
				{
					return true;
				}
			}
			return false;
		}
		
		static public function getItemIndex(array:Array, item:Object):Number
		{
			for(var i:Number=0; i<array.length; i++){
				if(item==array[i])
				{
					return i;
				}
			}
			return -1;
		}
	
		static public function difference(minuend:Array, substradent:Array):Array
		{
			//trace(toString() + ".difference(" + arguments + ")");
			
			var localResult:Array = minuend.concat();
			var localSubstradent:Array = substradent.concat();
			
			while(localSubstradent.length>0){
				var localCurrentValue:Object = localSubstradent.shift();
				for(var i:Number=0; i<localResult.length; i++){
					if(localResult[i]==localCurrentValue){
						localResult.splice(i, 1);
						break;
					}
				}
			}
			
			return localResult;
		}
	
		static public function compileArray(arrayLength:Number, elementsCreator:Function):Array
		{
			var arr:Array = new Array();
			for(var i:Number=0; i<arrayLength; i++){
				arr[i] = (elementsCreator as Boolean) ? elementsCreator(i) : i;
			}
			return arr;
		}
		
		
		static public function clearArray(array:Array):void
		{
			while(array.length>0)
			{
				array.pop();
			}
		}
	
	
		static public function objectToArray(object:Object, useKeys:Boolean):Array
		{
			var localArray:Array = new Array();
			for(var propertyName:String in object){
				if(useKeys){
					localArray.push(propertyName);
				}else{
					localArray.push(object[propertyName]);
				}
			}
			return localArray;
		}
	
	
		static public function joinToString(array:Array, tabString:String, objectTraceTabString:String):String
		{
			tabString = tabString || "";
			objectTraceTabString = objectTraceTabString || "";
			var joinString:String = "";
			for(var i:Number=0; i<array.length; i++){
				if(joinString.length){joinString += ",";}
				joinString += tabString + array[i].toString(objectTraceTabString);
			}
			return joinString;
		}
	}
}